import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/index.vue'
import Home from '@/views/home/index.vue'
import Layout from '@/views/layout/index.vue'
import Article from '@/views/article/'
import Publish from '@/views/publish/'
import Image from '@/views/image/'
Vue.use(VueRouter)
// 路由配置表
const routes = [
  { path: '/login', name: 'login', component: Login },
  // { path: '/', name: 'home', component: Home },
  { path: '/', component: Layout, children: [{ path: '', name: 'home', component: Home }, { path: '/article', name: 'article', component: Article }, { path: '/publish', name: 'publish', component: Publish }, { path: '/image', name: 'image', component: Image }] }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const user = JSON.parse(window.localStorage.getItem('user'))
  if (to.path !== '/login') {
    if (user) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
